package com.jwell.panprovider.ui.dialog

import android.app.Dialog
import android.content.Context
import android.content.DialogInterface
import android.view.LayoutInflater
import android.view.View
import android.view.Window
import android.view.WindowManager
import android.widget.Toast
import com.jwell.panprovider.R
import com.jwell.panprovider.application.App
import kotlinx.android.synthetic.main.item_transport_plane_info_dialog_layout.view.*

/**
 * Created by 76577 on 2018/5/3.
 * 供应详情填写预登记供应车数
 */
class TransportPlaneInfoDialog {
    class Builder(private var mContext: Context) {
        private val isOutTouch = false
        private var mEnsureOnclickListener: EnSureOnClickListener? = null


        fun setOnEnsureClickListener(listener: EnSureOnClickListener?): Builder {
            mEnsureOnclickListener = listener
            return this
        }

        fun create(): Dialog {
            val dialog = Dialog(mContext, R.style.dialog)
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
            val convertView: View = LayoutInflater.from(mContext).inflate(R.layout.item_transport_plane_info_dialog_layout, null)
            with(convertView) {
                edit_carNum.isFocusable = true
                edit_carNum.isFocusableInTouchMode = true
                edit_carNum.requestFocus()
                dialog.window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE)
                bt_noTitle_cancel.setOnClickListener {
                    dialog.dismiss()
                }
                bt_noTitle_ensure.setOnClickListener {
                    if (edit_carNum.text.isNullOrEmpty()) {
                        Toast.makeText(App.instance, "请填写预计供应车数", Toast.LENGTH_SHORT).show()
                        return@setOnClickListener
                    }
                    if ("" == edit_carNum.text.toString()) {
                        Toast.makeText(App.instance, "请填写预计供应车数", Toast.LENGTH_SHORT).show()
                        return@setOnClickListener
                    }
                    try {
                        if (edit_carNum.text.toString().toInt() <= 0) {
                            Toast.makeText(App.instance, "请填写大于0供应车数", Toast.LENGTH_SHORT).show()
                            return@setOnClickListener
                        }
                    } catch (e: Exception) {
                        e.printStackTrace()
                        Toast.makeText(App.instance, "请填写正确的供应车数", Toast.LENGTH_SHORT).show()
                        return@setOnClickListener
                    }

                    mEnsureOnclickListener?.onClick(edit_carNum.text.toString().toInt())
                    dialog.dismiss()
                }
                dialog.setContentView(convertView)
                dialog.setCanceledOnTouchOutside(isOutTouch)
                return dialog
            }
        }
    }

    interface EnSureOnClickListener {
        fun onClick(carNum: Int)
    }
}